using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace Nivlag
{
	public partial class SpdGenericInterfaceActivity: Activity
	{
        public static DependencyProperty DispatchFunctionProperty
    = System.Workflow.ComponentModel.DependencyProperty.Register(
    "DispatchFunction", typeof(string), typeof(SpdGenericInterfaceActivity));

        public static DependencyProperty InParam1Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam1", typeof(string), typeof(SpdGenericInterfaceActivity));

        public static DependencyProperty InParam2Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam2", typeof(string), typeof(SpdGenericInterfaceActivity));

        public static DependencyProperty InParam3Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam3", typeof(string), typeof(SpdGenericInterfaceActivity));

        public static DependencyProperty OutResult1Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "OutResult1", typeof(string), typeof(SpdGenericInterfaceActivity));

        public static DependencyProperty OutResult2Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "OutResult2", typeof(string), typeof(SpdGenericInterfaceActivity));


        public static DependencyProperty InDate1Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InDate1", typeof(System.DateTime), typeof(SpdGenericInterfaceActivity));



        public SpdGenericInterfaceActivity()
		{
			InitializeComponent();
		}

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string DispatchFunction
        {
            get
            {
                return ((string)(base.GetValue(SpdGenericInterfaceActivity.DispatchFunctionProperty)));
            }
            set
            {
                base.SetValue(SpdGenericInterfaceActivity.DispatchFunctionProperty, value);
            }

        } // public string DispatchFunction

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public System.DateTime InputDate1
        {
            get
            {
                return ((System.DateTime)(base.GetValue(SpdGenericInterfaceActivity.DispatchFunctionProperty)));
            }
            set
            {
                base.SetValue(SpdGenericInterfaceActivity.InDate1Property, value);
            }

        } // public string InputDate1

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam1
        {
            get
            {
                return ((string)(base.GetValue(SpdGenericInterfaceActivity.InParam1Property)));
            }
            set
            {
                base.SetValue(SpdGenericInterfaceActivity.InParam1Property, value);
            }
        } // public string InParam1

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam2
        {
            get
            {
                return ((string)(base.GetValue(SpdGenericInterfaceActivity.InParam2Property)));
            }
            set
            {
                base.SetValue(SpdGenericInterfaceActivity.InParam2Property, value);
            }
        } // public string InParam2

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam3
        {
            get
            {
                return ((string)(base.GetValue(SpdGenericInterfaceActivity.InParam3Property)));
            }
            set
            {
                base.SetValue(SpdGenericInterfaceActivity.InParam3Property, value);
            }
        } // public string InParam3

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OutResult1
        {
            get
            {
                return ((string)(base.GetValue(SpdGenericInterfaceActivity.OutResult1Property)));
            }
            set
            {
                base.SetValue(SpdGenericInterfaceActivity.OutResult1Property, value);
            }
        } // public string OutResult1

        
        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OutResult2
        {
            get
            {
                return ((string)(base.GetValue(SpdGenericInterfaceActivity.OutResult2Property)));
            }
            set
            {
                base.SetValue(SpdGenericInterfaceActivity.OutResult2Property, value);
            }
        } // public string OutResult2


        protected override ActivityExecutionStatus
          Execute(ActivityExecutionContext executionContext)
        {

            string functionToDispatch;
            functionToDispatch = this.DispatchFunction.ToLower();

           
            switch (functionToDispatch)
            {

                #region Contains()
                // Added 11/09/07
                // Tested: NO
                case "contains()":
                    {
                        this.OutResult1 = "false";
                        if (this.InParam1.Contains(InParam2)) this.OutResult1 = "true";
                        break;
                    } // contains

                #endregion

                #region EndsWith()

                // Added 11/09/07
                // Tested: NO
                case "endswith()":
                    {
                        this.OutResult1 = "false";
                        if (this.InParam1.EndsWith(InParam2)) this.OutResult1 = "true";
                        break;
                    } // endswith

                #endregion

                #region Entry()
                // Added 11/09/07
                // "entry()" returns the n^th entry in a delimited string.
                // InParam1: A delimited string.
                // InParam2: Index in the list of entries (0 = first entry)
                // InParam3: Delimiter to use (e.g. a comma).
                // Example: 
                //   InParam1: a,b,c,d,e,f
                //   InParam2: 3
                //   InParam3: ,
                //   OutResult1: "d"
                // Tested: NO
                case "entry()":
                    {
                        try
                        {
                            // Create an "entries" array.  Because I'm a bit lazy,
                            // initialize the length to the size of the unparsed
                            // string.  There cannot be any more entries that that.
                            string[] entries = new string[InParam1.Length];

                            try
                            {
                                // Load up the supplied delimiter
                                char[] splitter = new char[1];
                                splitter[0] = Convert.ToChar(InParam3);

                                // Split the incoming array into pieces.
                                entries = this.InParam1.Split(splitter);

                                try
                                {
                                    // Get the entry we wanted.
                                    OutResult1 = entries[Convert.ToInt32(InParam2)];
                                }
                                catch (Exception e)
                                {
                                    OutResult1 = "ERROR: Failed to Convert or otherwise use InParam2:  " +
                                        e.ToString();
                                    break;
                                } // catch error on convert.ToInt32
                            }
                            catch (Exception e)
                            {
                                this.OutResult1 = "ERROR: Failed to split using the supplied delimiter. " +
                                    e.ToString();
                            } // catch 

                        } // overall try
                        catch (Exception e)
                        {
                            this.OutResult1 = "ERROR: Failed to initialize entries array: " + e.ToString();
                            break;
                        } // catch 

                        break;
                    } // case "entry()"
                #endregion

                #region Length()
                // Added 11/09/07
                // Tested: NO
                case "length()":
                    {
                        this.OutResult1 = this.InParam1.Length.ToString();
                        break;
                    }
                #endregion

                #region Num-entries()
                // Added 11/09/07
                // "num-entries()" returns the number of entries in a delimited list
                // InParam1: A delimited string.
                // InParam2: Delimiter to use (e.g. a comma).
                // Example: 
                //   InParam1: a,b,c,d,e,f
                //   InParam2: ,
                //   OutResult1: "6"
                // Tested: NO
                case "num-entries()":
                    {
                        try
                        {
                            // Load up the supplied delimiter
                            char[] splitter = new char[1];
                            splitter[0] = Convert.ToChar(InParam2);

                            // Split the incoming array into pieces and put the length of that array into the result.
                            OutResult1 = Convert.ToString(this.InParam1.Split(splitter).Length);

                        }
                        catch (Exception e)
                        {
                            this.OutResult1 = "ERROR: Failed to split using the supplied delimiter. " +
                                e.ToString();
                        } // catch 

                        break;
                    } // case "num-entries()"

                #endregion

                #region Replace()
                // Added 11/09/07
                // Tested: NO
                case "replace()":
                    {
                        this.OutResult1 = this.InParam1.Replace(InParam2, InParam3);
                        break;
                    }

                #endregion

                #region StartsWith()
                // Added 11/09/07
                // Tested: NO
                case "startswith()":
                    {
                        this.OutResult1 = "false";
                        if (this.InParam1.StartsWith(InParam2)) this.OutResult1 = "true";
                        break;
                    } // startswith
                #endregion

                #region Substring(start,length)
                // Added 11/09/07
                // Tested: NO
                // Returns the substring of a string between [start] and [end]
                case "substring(start,length)":
                    {
                        try
                        {
                            this.OutResult1 = this.InParam1.Substring(Convert.ToInt32(InParam2), Convert.ToInt32(InParam3));
                        }
                        catch (Exception e)
                        {
                            OutResult1 = "ERROR: Substring(start,length) failed: " + e.ToString();
                        }
                        break;
                    }

                #endregion

                #region Substring(start)
                // Added 11/09/07
                // Tested: NO
                // Returns the substring of a string from [start] to the end of the string.
                case "substring(start)":
                    {
                        try
                        {
                            this.OutResult1 = this.InParam1.Substring(Convert.ToInt32(InParam2));
                        }
                        catch (Exception e)
                        {
                            OutResult1 = "ERROR: Substring(start) failed: " + e.ToString();
                        }
                        break;
                    }

                #endregion

                #region ToLower()
                // Initial release.
                // Tested: YES
                case "tolower()":
                    {
                        this.OutResult1 = this.InParam1.ToLower();
                        break;
                    }

                #endregion

                #region ToUpper()

                // Added 11/09/07
                // Tested: NO
                case "toupper()":
                    {
                        this.OutResult1 = this.InParam1.ToUpper();
                        break;
                    } // toupper

                #endregion

                default:
                    {
                        this.OutResult1 = "Unknown function: [" + this.DispatchFunction + "].";
                        break;
                    } // default
            }

            return ActivityExecutionStatus.Closed;
        } // ActivityExecutionStatus

	}

}

